using SqlQueryBuilder.Util;

namespace SqlQueryBuilder.Clauses
{
    /// <summary>
    /// Like Clause
    /// </summary>
    public class Like : IConditionalClause
    {
        private readonly LikeType likeType;
        private readonly string field;
        private readonly string value;

        /// <summary>
        /// Initializes a new instance of the <see cref="Like"/> class.
        /// </summary>
        /// <param name="field">The field.</param>
        /// <param name="value">The value.</param>
        /// <param name="likeType">Type of the like.</param>
        public Like(string field, string value, LikeType likeType)
        {
            this.field = field;
            this.likeType = likeType;
            this.value = value;
        }

        /// <summary>
        /// Builds the where clause.
        /// </summary>
        /// <returns></returns>
        public string BuildConditionalClause()
        {
            return CreateClause();
        }

        /// <summary>
        /// Creates the clause.
        /// </summary>
        /// <returns></returns>
        private string CreateClause() {
            switch (likeType)
            {
                case LikeType.Left:
                    return string.Format("{0} LIKE '{1}%'", field, value);

                case LikeType.Right:
                    return string.Format("{0} LIKE '%{1}'", field, value);

                default:
                    return string.Format("{0} LIKE '%{1}%'", field, value);
            }
        }
    }
}